﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Atom.cache;
using Atom.NetFrame;

namespace Atom.biz.impl
{
    public class AccountBiz : IAccountBiz
    {
        private IAccountCache accountCache = CacheFactory.AccountCache; 
        public void Close(UserToken ut)
        {
            accountCache.OffLine(ut);
        }
        public int GetID(UserToken ut)
        {
            return accountCache.GetId(ut);
        }

        public int Create(UserToken ut, string userName, string password)
        {
            if (accountCache.HasAccount(userName)) return 1;
            accountCache.Add(userName, password);
            return 0;
        }
        public int Login(UserToken ut, string usetName, string password)
        {
            //账号密码为空
            if (usetName == null || password == null) return 4;
            //账号不存在,则无法登陆
            if (!accountCache.HasAccount(usetName)) return 1;
            //有相同账号在线 
            if (accountCache.isOnLine(usetName)) return 2;
            //账号密码不匹配 
            if (!accountCache.Match(usetName, password)) return 3;
            accountCache.OnLine(ut, usetName);
            return 0;

        }
    }
}
